package com.isczy.eduservice.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.isczy.eduservice.entity.EduChapter;
import com.isczy.eduservice.entity.EduVideo;
import com.isczy.eduservice.entity.vo.ChapterVo;
import com.isczy.eduservice.mapper.EduChapterMapper;
import com.isczy.eduservice.mapper.EduVideoMapper;
import com.isczy.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author CZY
 * @since 2021-01-02
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoMapper videoMapper;

    /***
     * ***************************************
     * 功能描述:课程大纲列表,根据课程id进行查询
     * @param courseId
     * @return {@link List< ChapterVo>}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-05 17:19】
     * @author modify:
     */
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1 根据课程id查询课程里面所有的章节
        List<ChapterVo> eduChapterList = baseMapper.selectChapterVoList(courseId);
        eduChapterList.forEach(chapterVo -> {
            if (ArrayUtil.isNotEmpty(eduChapterList)){
                //2.根据章节id查询所有小节
                chapterVo.setChildren(videoMapper.selectVideoVoList(chapterVo.getId()));
            }
        });
        return eduChapterList;
    }

    /***
     * ***************************************
     * 功能描述:删除章节
     * @param chapterId
     * @return
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-05 17:19】
     * @author modify:
     */
    @Override
    public int deleteChapter(String chapterId) {
        //根据章节id 查询小节表，如果查询到存在小节数据，不进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoMapper.selectCount(wrapper);
        if(count <=0) {
            baseMapper.deleteById(chapterId);
        }
        return count;
    }
}
